/*
 * Copyright (C) 2010 Imavis Srl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.imavis.video.parse.mpeg4.impl;

import com.imavis.video.parse.impl.BitBuffer;
import java.nio.ByteBuffer;

/**
 *
 * @author Aldo Brucale
 */
final class Mpeg4Utils {
    private static final boolean STRICT_PARSING = false;

    public static void checkMarkerBit(final BitBuffer b) {
        if (!b.getBit()) {
            if (STRICT_PARSING) {
                throw new IllegalArgumentException("Marker bit expected");
            }
        }
    }

    /**
     * @param state an integer
     * @return true if the integer begins with 0x000001
     */
    public static boolean isStartCode(final int state) {
        return (state & 0xFFFFFF00) == 0x0000000100;
    }

    /**
     * Aggiorna
     *
     * @param bb
     * @return
     */
    public static int seekStartCode(final ByteBuffer bb) {
        int state = 0xFFFFFFFF;
        while (bb.hasRemaining()) {
            state <<= 8;
            state |= (bb.get() & 0xff);
            if (isStartCode(state)) {
                return state & 0xFF;
            }
        }
        return -1;
    }

    private Mpeg4Utils() {
    }
}
